<?php

namespace Jammiest\SwigeCore\Handlers;

use Jammiest\SwigeCore\Interface\EventHandlerInterface;

class EventHandler implements EventHandlerInterface
{
    public function __construct(protected \Psr\Container\ContainerInterface $container)
    {
        
    }
    public function onOpen($server, $request) {
        echo "Connection opened." . PHP_EOL;
    }


    public function onTimer($server, $interval) {
        echo "Timer triggered: Interval={$interval}" . PHP_EOL;
    }

    public function onWorkerMessage($server, $workerId, $message) {
        echo "WorkerMessage: WorkerId={$workerId}, Message={$message}" . PHP_EOL;
    }

    public function onHttp2Close($server, $fd) {
        echo "HTTP2 connection closed: FD={$fd}" . PHP_EOL;
    }

    public function onSslHandshake($server, $fd) {
        echo "SSL Handshake: FD={$fd}" . PHP_EOL;
    }

    public function onSslClose($server, $fd) {
        echo "SSL connection closed: FD={$fd}" . PHP_EOL;
    }

    public function onStart($server) {
        echo "Server: onStart." . PHP_EOL;
    }

    public function onRequestBody($server, $request) {
        echo "Request Body received." . PHP_EOL;
    }

    public function onRequestHeaders($server, $request) {
        echo "Request Headers received." . PHP_EOL;
    }

    public function onBeforeShutdown($server) {
        echo "Server: onBeforeShutdown." . PHP_EOL;
    }

    public function onTaskWorkerStart($server, $workerId) {
        echo "TaskWorkerStart: WorkerId={$workerId}" . PHP_EOL;
    }

    public function onTaskWorkerStop($server, $workerId) {
        echo "TaskWorkerStop: WorkerId={$workerId}" . PHP_EOL;
    }

    public function onManagerStart($server) {
        echo "Manager: onManagerStart." . PHP_EOL;
    }

    public function onManagerStop($server) {
        echo "Manager: onManagerStop." . PHP_EOL;
    }

    public function onShutdown($server) {
        echo "Server: onShutdown." . PHP_EOL;
    }

    public function onWorkerStart($server, $workerId) {
        echo "WorkerStart: WorkerId={$workerId}" . PHP_EOL;
    }

    public function onWorkerStop($server, $workerId) {
        echo "WorkerStop: WorkerId={$workerId}" . PHP_EOL;
    }

    public function onWorkerError($server, $workerId, $exitCode, $signal) {
        echo "WorkerError: WorkerId={$workerId}, ExitCode={$exitCode}, Signal={$signal}" . PHP_EOL;
    }
    public function onConnect($server, $fd, $reactorId) {
        echo "Client: Connect." . PHP_EOL;
    }
    public function onClose($server, $fd) {
        echo "Client: Close." . PHP_EOL;
    }
    public function onTask($server, $taskId, $workerId, $data)
    {
        echo "New AsyncTask[id={$taskId}]" . PHP_EOL;
        //返回任务执行的结果
        $server->finish("{$data} -> OK");
    }
    public function onFinish($server, $taskId, $data)
    { {
            echo "AsyncTask[{$taskId}] Finish: {$data}" . PHP_EOL;
        }
    }
    public function onPipeMessage($server, $fromWorkerId, $message) {
        echo "Message from {$fromWorkerId}: {$message}" . PHP_EOL;
    }
    public function onRequest($server, $request, $response) {
        $response->end("Hello World\n");
    }
    public function onMessage($server, $frame) {
        echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
        $server->push($frame->fd, "this is server");
    }
    public function onHandShake($request, $response) {
        echo "HandShake: FD={$request->fd}" . PHP_EOL;
    }
    public function onReceive($server, $fd, $reactorId, $data) {
        $server->send($fd, "Swoole: {$data}");
    }
    public function onWsClose($server, $fd) {
        echo "Client: Close." . PHP_EOL;
    }
    public function onHttpRequest($request, $response) {
        $response->end("Hello World\n");
    }

    public function onTcpReceive($server, $fd, $reactorId, $data) {
        $server->send($fd, "Swoole: {$data}");
    }
    public function onTcpClose($server, $fd) {
        echo "Client: Close." . PHP_EOL;
    }
    public function onTcpConnect($server, $fd, $reactorId) {
        echo "Client: Connect." . PHP_EOL;
    }
}
